#include "engine.h"

EventChecker::EventChecker() {
}

void EventChecker::do_events() {
	SDL_Event event_data;
	int flag;
	unsigned long event_fingerprint = 0;
	map< unsigned long, map<string,string,ltstr_stl>* >::iterator fingerprint_iter;
	map<string,string,ltstr_stl>::iterator assoc_iter;
	do {
		flag = SDL_PollEvent(&event_data);
		if(!flag) continue;
		switch(event_data.type) {
			case SDL_KEYDOWN:
				event_fingerprint |=  ( TYPE_KEYBOARD | KEYPRESSED ); break;	
			case SDL_KEYUP:
				event_fingerprint |=  ( TYPE_KEYBOARD | KEYRELEASED ); break;
			case SDL_MOUSEMOTION:
				event_fingerprint |=  ( TYPE_MOUSEMOVE ); break;
			case SDL_MOUSEBUTTONDOWN:
				event_fingerprint |=  ( TYPE_MOUSEBUTTON | MOUSEPRESSED ); break;
			case SDL_MOUSEBUTTONUP:
				event_fingerprint |=  ( TYPE_MOUSEBUTTON ); break;	
		};	
	        	
	}while(flag);
	if( event_fingerprint == 0) return; /* Nothing to process */
	for(fingerprint_iter = Util::events.repository.begin(); fingerprint_iter != Util::events.repository.end(); fingerprint_iter++) {
		if(fingerprint_iter->first & event_fingerprint == event_fingerprint) {
			for(assoc_iter = fingerprint_iter->second->begin(); assoc_iter != fingerprint_iter->second->end(); assoc_iter++) {
				Util::events.script_land.call(assoc_iter->second,assoc_iter->first); /* call name , arg(obj name) */
			}
		}
	}
}
